/**
 * 
 */
package org.knoppers.examples;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

/**
 * @author jkonopka
 *
 */
public class FileTriggerExample {
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		final Path _directotyToWatch = Paths.get(System.getProperty("user.home"));
		final WatchService watcherSvc = FileSystems.getDefault().newWatchService();
		WatchKey watchKey = _directotyToWatch.register(watcherSvc, StandardWatchEventKinds.ENTRY_CREATE,
				StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

		while (true) {
			watchKey = watcherSvc.take();
			for (final WatchEvent<?> event : watchKey.pollEvents()) {
				@SuppressWarnings("unchecked")
				final WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
				System.out.println(event.kind().name().toString() + " "
						+ _directotyToWatch.resolve(watchEvent.context()));
				watchKey.reset();
			}
		}
		
	}

}
